package com.wngbms.netty._04groupchat.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;

import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;

public class ChatServerHandler extends SimpleChannelInboundHandler<String> {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public ChatServerHandler() {
        System.out.println(Thread.currentThread().getName() + " ChatServerHandler");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();

        String id = getId(channel);

        ChannelGroups.add(id, channel);
        ChannelGroups.broadCast(id, "[客户端id:"+id +"]" + channel.remoteAddress() + " 加入聊天" + sdf.format(new java.util.Date()) + " \n");
        System.out.println("handlerAdded id:" + id + ", size:" + ChannelGroups.size(id));
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();

        InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress();
        int port = socketAddress.getPort();
        String id = Integer.toString(port % 3);

        ChannelGroups.discard(id, channel);
        ChannelGroups.broadCast(id, "[客户端id:"+id +"]" + channel.remoteAddress() + " 离开了\n");
        System.out.println("handlerRemoved id:" + id + ", size:" + ChannelGroups.size(id));

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(getId(channel) + " " + channel.remoteAddress() + " 上线了~");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(getId(channel) + " " + channel.remoteAddress() + " 离线了~");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        Channel channel = ctx.channel();

        String id = getId(channel);

        ChannelGroup channelGroup = ChannelGroups.get(id);
        if (channelGroup != null) {
            for (Channel ch : channelGroup) {
                if(channel != ch) { //不是当前的channel,转发消息
                    ch.writeAndFlush("[客户]" + channel.remoteAddress() + " 发送了消息" + msg + "\n");
                }else {//回显自己发送的消息给自己
                    ch.writeAndFlush("[自己]发送了消息" + msg + "\n");
                }
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    public static String getId(Channel channel) {
        InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress();
        int port = socketAddress.getPort();
        String id = Integer.toString(port % 3);
        return id;
    }

}
